package com.atguigu.acl.controller;


import com.atguigu.acl.dto.MenuNode;
import com.atguigu.acl.entity.Menu;
import com.atguigu.acl.service.MenuService;
import com.atguigu.common.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author smday
 * @since 2020-08-10
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("添加后台菜单")
    @PostMapping("/create")
    public Result create(@RequestBody Menu menu) {
        int count = menuService.create(menu);
        if (count > 0) {
            return Result.ok().data("count", count);
        }
        return Result.error();
    }

    @ApiOperation("修改后台菜单")
    @PostMapping("/update/{id}")
    public Result update(@PathVariable Long id,
                         @RequestBody Menu menu) {
        int count = menuService.update(id, menu);
        if (count > 0) {
            return Result.ok().data("count", count);
        }
        return Result.error();
    }

    @ApiOperation("根据ID获取菜单详情")
    @GetMapping("/{id}")
    public Result getItem(@PathVariable Long id) {
        Menu menu = menuService.getItem(id);
        return Result.ok().data("menu", menu);
    }

    @ApiOperation("根据ID删除后台菜单")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        int count = menuService.delete(id);
        if (count > 0) {
            return Result.ok().data("count", count);
        }
        return Result.error();
    }

    @ApiOperation("分页查询后台菜单")
    @GetMapping("/list/{parentId}")
    public Result list(@PathVariable Long parentId,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Map<String, Object> map = menuService.list(parentId, pageSize, pageNum);
        return Result.ok().data(map);
    }

    @ApiOperation("树形结构返回所有菜单列表")
    @GetMapping("/treeList")
    public Result treeList() {
        List<MenuNode> list = menuService.treeList();
        return Result.ok().data("list", list);
    }

    @ApiOperation("修改菜单显示状态")
    @PostMapping("/updateHidden/{id}")
    public Result updateHidden(@PathVariable Long id, @RequestParam("hidden") Integer hidden) {
        int count = menuService.updateHidden(id, hidden);
        if (count > 0) {
            return Result.ok().data("count", count);
        }
        return Result.error();
    }

}

